<template>
  <div class="brand-list-page">
    <div class="page-header">
      <h1>品牌列表</h1>
    </div>
    <div class="content-card">
      <el-table :data="brandList" v-loading="loading" style="width: 100%">
        <el-table-column prop="index" label="序号" width="80">
          <template slot-scope="scope">
            {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="brandName" label="品牌"></el-table-column>
        <el-table-column prop="brandGender" label="性别"></el-table-column>
        <el-table-column prop="brandStatus" label="禁用状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.brandStatus"
              :active-value="1"
              :inactive-value="0"
              disabled
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="brandRemark" label="备注"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showEditDialog(scope.row.brandId)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹窗 -->
    <el-dialog title="编辑品牌" :visible.sync="editDialogVisible" width="500px" @close="resetEditForm">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="品牌名称">
          <el-input v-model="editForm.brandName"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="editForm.brandGender" placeholder="请选择">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="禁用状态">
          <el-switch v-model="editForm.brandStatus" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editForm.brandRemark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'BrandList',
  data() {
    return {
      brandList: [],
      loading: false,
      currentPage: 1,
      pageSize: 5,
      total: 0,
      // 编辑弹窗
      editDialogVisible: false,
      editForm: {
        brandId: null,
        brandName: '',
        brandGender: '',
        brandStatus: 0,
        brandRemark: ''
      }
    }
  },
  mounted() {
    this.fetchBrandList()
  },
  methods: {
    // 分页查询品牌列表
    async fetchBrandList() {
      this.loading = true
      try {
        const response = await axios.get('/api/brand/pageList', {
          params: {
            pageNum: this.currentPage,
            pageSize: this.pageSize
          }
        })
        if (response.data.code === 200) {
          this.brandList = response.data.data.list
          this.total = response.data.data.total
        } else {
          this.$message.error(response.data.msg || '获取品牌列表失败')
        }
      } catch (error) {
        this.$message.error('获取品牌列表失败')
      } finally {
        this.loading = false
      }
    },
    handleCurrentChange(page) {
      this.currentPage = page
      this.fetchBrandList()
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.currentPage = 1
      this.fetchBrandList()
    },
    // 显示编辑弹窗并查询品牌详情
    async showEditDialog(brandId) {
      try {
        const response = await axios.get('/api/brand/getBrandById', {
          params: { id: brandId }
        })
        if (response.data.code === 200) {
          this.editForm = { ...response.data.data }
          this.editDialogVisible = true
        } else {
          this.$message.error(response.data.msg || '获取品牌信息失败')
        }
      } catch (error) {
        this.$message.error('获取品牌信息失败')
      }
    },
    // 保存编辑
    async handleEditSave() {
      try {
        const response = await axios.post('/api/brand/updateBrand', this.editForm)
        if (response.data.code === 200) {
          this.$message.success('修改成功')
          this.editDialogVisible = false
          this.fetchBrandList()
        } else {
          this.$message.error(response.data.msg || '修改失败')
        }
      } catch (error) {
        this.$message.error('修改失败')
      }
    },
    // 重置编辑表单
    resetEditForm() {
      this.editForm = {
        brandId: null,
        brandName: '',
        brandGender: '',
        brandStatus: 0,
        brandRemark: ''
      }
    }
  }
}
</script>

<style scoped>
.brand-list-page { padding: 20px; }
.page-header { margin-bottom: 20px; }
.page-header h1 { color: #333; font-size: 24px; margin: 0; }
.content-card { background: white; border-radius: 10px; padding: 30px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);}
.pagination-container { margin-top: 20px; text-align: center; }
.dialog-footer { text-align: right; }
</style>